package cn.com.fuhuitong.main.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import com.alibaba.android.vlayout.LayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.home.entity.CaseEntity
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_case_list_image.view.*
import kotlinx.android.synthetic.main.item_case_list_image_content.view.*
import kotlinx.android.synthetic.main.item_case_list_video.view.*
import kotlinx.android.synthetic.main.item_tab_layout.view.*

class CaseTabAdapter(
    private val mContext: Context,
    mHelper: LayoutHelper,
    tabData: MutableList<String>
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_tab_layout,
    mItemViewType = ItemViewType.CASE_TAB
) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    init {
        setAddDataNew(tabData)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.itemView.text_tab_layout_name.text = data[position]
        holder.itemView.text_tab_layout_name.setTextColor(
            mContext.resources.getColor(
                if (holder.layoutPosition == selectedPosition) R.color.colorMain else R.color.colorMainGrey
            )
        )
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class CaseVideoAdapter(
    private val mContext: Context, mHelper: LayoutHelper
) : BaseDelegateAdapter<CaseEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_case_list_video,
    mItemViewType = ItemViewType.CASE_LIST
) {

    init {
        addChildClickViewIds(R.id.text_case_list_video_play)
    }



    override fun onBindViewHolder(itemView: View, position: Int, itemData: CaseEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        Glide.with(itemView).load(getImageUrl(itemData.head_image))
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(itemView.image_case_list_video_heads)

        Glide.with(itemView).load(getImageUrl(itemData.image))
            .into(itemView.image_case_list_video_icon)

        itemView.text_case_list_video_name.text = itemData.send_name
        itemView.text_case_list_video_time.text =
            TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd")
        itemView.text_case_list_video_title.text = itemData.title
    }
}

class CaseImageAdapter(
    private val mContext: Context, mHelper: LayoutHelper
) : BaseDelegateAdapter<CaseEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_case_list_image,
    mItemViewType = ItemViewType.CASE_LIST
) {

    override fun onBindViewHolder(itemView: View, position: Int, itemData: CaseEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        Glide.with(itemView).load(getImageUrl(itemData.head_image))
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(itemView.image_case_list_image_heads)

        itemView.text_case_list_image_name.text = itemData.send_name
        itemView.text_case_list_image_time.text =
            TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd")
        itemView.text_case_list_image_title.text = itemData.title

        itemView.recycler_case_list_image.layoutManager =
            GridLayoutManager(mContext, 3).apply {
            }
        val images: List<String> = itemData.images.split(",")
        itemView.recycler_case_list_image.adapter =
            ImageAdapter(mContext, images)
    }

    private class ImageAdapter(
        private val mContext: Context,
        private val images: List<String>
    ) : RecyclerView.Adapter<RecyclerViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext)
                    .inflate(R.layout.item_case_list_image_content, parent, (false))
            )
        }

        override fun getItemCount(): Int =
            if (images.size > 9) 9 else images.size

        override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
            Glide.with(holder.itemView).load(getImageUrl(images[position]))
                .into(holder.itemView.image_case_list_image_icon)
        }

    }
}
